package com.example.shushiyunsst.fragment.main.home.recycler.provider;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.library_common.adapter.JssBaseQuickAdapter;
import com.example.library_common.adapter.JssBaseViewHolder;
import com.example.library_common.adapter.multi.AdapterViewItemViewClick;
import com.example.library_common.adapter.multi.JssMultiItemViewHolder;
import com.example.library_common.adapter.multi.JssNewMultipleAdapter;
import com.example.library_common.utils.QMUIDisplayHelper;
import com.example.shushiyunsst.R;
import com.example.shushiyunsst.fragment.main.home.recycler.bean.AdviseEntity;
import com.example.shushiyunsst.fragment.main.home.recycler.bean.CurrencyEntityWrapper;
import com.example.shushiyunsst.fragment.main.home.recycler.bean.MiddleImgEntity;
import com.example.shushiyunsst.fragment.main.home.recycler.bean.MiddleImgEntityWrapper;
import com.example.wallet.global.AppGlobals;

import static com.example.shushiyunsst.util.RouterPathKt.MINER22;

public class MiddleImgProvider extends JssMultiItemViewHolder<MiddleImgEntityWrapper> implements OnItemClickListener {

    private final int height;

    private JssBaseQuickAdapter<MiddleImgEntity> mAdapter;

    public MiddleImgProvider(View view) {
        super(view);
        int screenWidth = QMUIDisplayHelper.getScreenWidth(AppGlobals.getApplication());
        height = screenWidth / 4;
    }

    @Override
    public int getItemLayout() {
        return R.layout.middle_img_provider;
    }

    @Override
    public void convert(Context context, MiddleImgEntityWrapper mWrapper) {
        super.convert(context, mWrapper);
        RecyclerView mRecyclerView = findView(R.id.mRecyclerView);
        if (mRecyclerView == null) return;
        mRecyclerView.setHasFixedSize(true);
        ViewGroup.LayoutParams lp = mRecyclerView.getLayoutParams();
        if (lp != null && lp.height != height) {
            lp.height = height;
            mRecyclerView.setLayoutParams(lp);
        }

        LinearLayoutManager manager =
                new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);

        mRecyclerView.setLayoutManager(manager);

        if (mAdapter == null) {
            mAdapter = new JssBaseQuickAdapter<MiddleImgEntity>(R.layout.middle_img_provider_item) {
                @Override
                protected void convert(JssBaseViewHolder helper, MiddleImgEntity item) {
                    super.convert(helper, item);
                    helper.setImageNetUrl(
                            R.id.img_cover,
                            item.getModUrl(),
                            R.drawable.img_erros_bg_rect
                    );
                }
            };

        }

        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setOnFlingListener(null);
        mAdapter.clears();
        mAdapter.addData(mWrapper.getList());
        mAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> subAdapter, @NonNull View view, int position) {
        if (mAdapter == null) return;
        MiddleImgEntity item = mAdapter.getItem(position);
        if (item != null && TextUtils.isEmpty(item.getTargetUrl())) {
            item.setTargetUrl(MINER22);
            item.setIntentTo(MiddleImgEntity.INTENT_TO_NATIVE);
        }

        if (adapter instanceof JssNewMultipleAdapter) {
            AdapterViewItemViewClick itemViewClick =
                    ((JssNewMultipleAdapter) adapter).getAdapterViewItemViewClick();
            if (itemViewClick != null) {
                itemViewClick.onAdapterViewListItemViewClick(item,item.getItemType());
            }
        }

    }
}
